package xin.marcher.module.search.mq.consumer;

import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.exception.MQClientException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import xin.marcher.module.common.constants.RocketMqConstant;
import xin.marcher.module.search.mq.config.RocketMQProperties;
import xin.marcher.module.search.mq.consumer.listener.ProductUpdateListener;


/**
 *
 */
@Configuration
public class ConsumerBeanConfig {

    /**
     * 配置内容对象
     */
    @Autowired
    private RocketMQProperties rocketMQProperties;


    /**
     * 监听商品修改的MQ消息
     *
     * @param productUpdateListener
     * @return
     * @throws MQClientException
     */
    @Bean("productUpdateTopic")
    public DefaultMQPushConsumer productUpdateTopic(ProductUpdateListener productUpdateListener) throws MQClientException {
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer(RocketMqConstant.PRODUCT_UPDATE_CONSUMER_GROUP);
        consumer.setNamesrvAddr(rocketMQProperties.getNameServer());
        consumer.subscribe(RocketMqConstant.PRODUCT_UPDATE_TOPIC, "*");
        consumer.registerMessageListener(productUpdateListener);
        consumer.start();
        return consumer;
    }
}
